
import request, { BASE_URL } from "./request"


export const login = (mobile, name, shop_id) => {
    return request.post("/api/auth/login", {
        mobile,
        name,
        shop_id
    })
}

export const canLoginUser = (mobile) =>
    request.get(`/api/auth/canLoginUser?mobile=${mobile}`)

export const getUserMessage = () =>
    request.get("/api/auth/user")

export const logout = () =>
    request.get("/api/auth/logout")

export const getLastTestMessage = () => {
    return request.get("/api/user/visionCheck/lastInfo")
}
/**
 * 获取所有理疗师和纪律
 * @returns 
 */
export const getTrainer = (shop_id) => {
    return request.get("/api/config/trainer?shop_id=" + shop_id)
}

export const submitVisionCheck = (result, shop_id, is_astigmatism) => {
    return request.post("/api/user/visionCheck/add", {
        shop_id,
        is_astigmatism: is_astigmatism ? "1" : "0",
        naked_vision: result[0] + "," + result[1],
        condenser_vision: result[2] + "," + result[3],
        astigmatism_left_result: is_astigmatism ? result[4] : undefined,
        astigmatism_right_result: is_astigmatism ? result[5] : undefined
    })
}

export const submitVisionCheck_test = (result, shop_id, is_astigmatism) => {
    return request.post("/api/user/visionCheck/add", {
        shop_id,
        is_astigmatism: is_astigmatism ? "1" : "0",
        naked_vision: result[0] + "," + result[1],
        condenser_vision: result[2] + "," + result[3],
        astigmatism_left_result: is_astigmatism ? result[4] : undefined,
        astigmatism_right_result: is_astigmatism ? result[5] : undefined,
        eye_test: {
            left_eye: [0.1, 0.3, 0.4, 0.5, 0.1],
            right_eye: [0.1, 0.3, 0.4, 0.5, 0.1]
        }
    })
}
/**
 * 发送签名图片
 */
export const sendSignImage = ({ filePath, success, fail }) => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            name: "file",
            url: BASE_URL + "/api/upload",
            success: (res) => {
                success && success(JSON.parse(res.data).data.path)
                resolve(JSON.parse(res.data).data.path)
            },
            filePath,
            fail: (err) => {
                fail && fail(err)
                reject(err)
            }
        })
    })
}

/**
 * 发送测试结果
 */
export const sendTestResult = ({
    shopId,
    glasses_vision,
    naked_vision,
    condenser_vision,
    inital_vision,
    is_astigmatism,
    astigmatism_result,
    issue,
    stage,
    signature,
    trainer_signature,
    morale_signature,
    trainer_id,
    morale_id
}) => {
    return request.post("/api/user/visionCheck/add", {
        shop_id: shopId,
        glasses_vision,
        naked_vision,
        condenser_vision,
        inital_vision: inital_vision || "",
        is_astigmatism,
        astigmatism_result,
        issue,
        stage,
        signature,
        trainer_signature,
        morale_signature,
        trainer_id,
        morale_id
    })
}
